package com.cy.api.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxyTests {
    public static void main(String[] args) {
        //1.目标对象
        NoticeService noticeService = new NoticeServiceImpl();
        //2.基于JDK方式为目标对象创建代理对象
        //2.1获取ClassLoader
        ClassLoader classLoader = noticeService.getClass().getClassLoader();
        //2.2获取目标对象实现的接口
        Class<?>[] interfaces = noticeService.getClass().getInterfaces();
        //2.3创建InvocationHandler(为代理对象处理业务的对象)
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //在此方法中如何调用目标对象的方法(反射)：目标对象，方法，参数
                Object result = method.invoke(noticeService, args);
              /*  System.out.println(proxy.getClass().getName());
                //System.out.println(method);
                Long[] array=(Long[])args[0];
                System.out.println(array[0]+","+array[1]+","+array[2]);*/
                return result;
            }
        };
        NoticeService proxy=
                (NoticeService) Proxy.newProxyInstance(
                        classLoader,interfaces,handler);
        System.out.println(proxy.getClass().getName());
        //调用代理对象的方法，底层会调用handler对象的invoke方法
        proxy.deleteById(10L,20L,30L);
    }
}
